package com.lyn.tool.utils;

import android.content.Context;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class FileUtil {

    private static class FileUtilTypeClass {
        private final static FileUtil instance = new FileUtil();
    }

    public static FileUtil getInstance() {
        return FileUtilTypeClass.instance;
    }

    private String pluginRootPath;


    public void init(Context context) {
        File filesDir = context.getFilesDir();
        pluginRootPath = createFolder(filesDir.getAbsolutePath().concat("/plugin"));
    }

    public String getPluginRootPath() {
        return pluginRootPath;
    }

    /**
     * 插件文件夹
     *
     * @param path 需要创建的文件夹路径
     * @return 返回创建的文件夹绝对路径
     */
    public String createFolder(String path) {
        File file = new File(path);
        if (!file.exists()) {
            boolean mkdirs = file.mkdirs();
            if (!mkdirs) {
                return null;
            }
        }
        return file.getAbsolutePath();
    }

    public File[] getFolders(String path) {
        File file = new File(path);
        if (!file.exists()) {
            return null;
        }
        if (file.isFile()) {
            return null;
        }
        return file.listFiles();
    }

    /**
     * 拷贝插件到插件目录
     *
     * @param path 插件文件绝对路径
     * @return 返回插件文件绝对路径
     */
    public String copyPluginApk(String path) {
        File file = new File(path);
        if (!file.exists()) {
            return null;
        }
        if (!file.isFile()) {
            return null;
        }
        return copyPluginApk(file);
    }

    /**
     * 拷贝插件到插件目录
     *
     * @param file 插件文件
     * @return 返回插件文件绝对路径
     */
    public String copyPluginApk(File file) {
        if (!file.exists()) {
            return null;
        }
        if (!file.isFile()) {
            return null;
        }
        String pluginFileName = getFileNameWithoutSuffix(file.getName());
        File pluginFileFolder = new File(pluginRootPath, pluginFileName);
        if (!pluginFileFolder.exists()) {
            boolean mkdirs = pluginFileFolder.mkdirs();
            if (!mkdirs) {
                return null;
            }
        }
        return copyFileToFolder(file, pluginFileFolder.getAbsolutePath());
    }


    /**
     * 获取不带后缀的文件名称
     *
     * @param fileName 文件名
     * @return 返回没有后缀的文件名
     */
    public  String getFileNameWithoutSuffix(String fileName) {
        return fileName.substring(0, fileName.lastIndexOf("."));
    }

    /**
     * 拷贝文件到指定目录
     *
     * @param path       文件路径
     * @param folderPath 目录路径
     * @return 返回拷贝后文件的绝对路径
     */
    public String copyFileToFolder(String path, String folderPath) {
        File file = new File(path);
        if (!file.exists()) {
            return null;
        }
        return copyFileToFolder(file, folderPath);
    }

    /**
     * 拷贝文件到指定目录
     *
     * @param file       文件
     * @param folderPath 目录路径
     * @return 返回拷贝后文件的绝对路径
     */
    public String copyFileToFolder(File file, String folderPath) {
        try {
            FileInputStream inputStream = new FileInputStream(file);
            File createFile = new File(folderPath, file.getName());
            if (!createFile.exists()) {
                boolean createFileNewFile = createFile.createNewFile();
                if (!createFileNewFile) {
                    return null;
                }
            }
            FileOutputStream outputStream = new FileOutputStream(createFile);
            byte[] data = new byte[1024];
            int length;
            while ((length = inputStream.read(data)) != -1) {
                outputStream.write(data, 0, length);
            }
            inputStream.close();
            outputStream.flush();
            outputStream.close();
            return createFile.getAbsolutePath();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
